import { isRoleNameExist, isUserNameExist } from '@/api/user'

const validateRoleNameExist = (rule, value, callback) => {
  isRoleNameExist({ roleName: value }).then(res => {
    if (res.resultData) {
      callback()
    } else {
      callback(new Error('角色已存在'))
    }
  })
}

const validateUserNameExist = (rule, value, callback) => {
  isUserNameExist({ userName: value }).then(res => {
    if (res.resultData) {
      callback()
    } else {
      callback(new Error('账号已存在'))
    }
  })
}

// 手机号
const validatePhone = (rule, value, callback) => {
  let err = '请输入正确的电话号码'
  const reg = /^((0\d{2,3}-\d{7,8})|(1[34578]\d{9}))$/

  if (value == '' || value == undefined || value == null) {
    callback()
  } else {
    if ((!reg.test(value)) && value != '') {
      callback(new Error(err))
    } else {
      callback()
    }
  }
}
// 邮箱
const validateEMail = (rule, value, callback) => {
  const reg = /^([a-zA-Z0-9]+[-_\.]?)+@[a-zA-Z0-9]+\.[a-z]+$/
  if (value == '' || value == undefined || value == null) {
    callback()
  } else {
    if (!reg.test(value)) {
      callback(new Error('请输入正确的邮箱地址'))
    } else {
      callback()
    }
  }
}
// 金额
const validateNum = (rule, value, callback) => {
  let patter = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
  if (!patter.test(value)) {
    return callback(new Error('请输入正确的金额格式'))
  } else {
    callback()//必须有此项回调，否则验证会一直不通过
  }
}
const validateNum0 = (rule, value, callback) => {
  if (value <= 0) {
    return callback(new Error('必须大于0'))
  } else {
    callback()//必须有此项回调，否则验证会一直不通过
  }
}

//中英文、数字
const validateChar = (rule, value, callback) => {
  let patter = /^[\u4e00-\u9fa5_a-zA-Z0-9]{4,10}/
  if (!patter.test(value)) {
    return callback(new Error('请输入正确格式'))
  } else {
    callback()//必须有此项回调，否则验证会一直不通过
  }
}

//中英文
const validChineseEnglishChar = (rule, value, callback) => {
  let patter = /[\u4E00-\u9FA5a-zA-Z]*/
  if (!patter.test(value)) {
    return callback(new Error('请输入正确格式'))
  } else {
    callback()//必须有此项回调，否则验证会一直不通过
  }
}

//验证英文字母、数字
const validateNumEnglish = (rule, value, callback) => {
  let patter = /[a-zA-Z_0-9]*/
  if (value) {
    if (!patter.test(value)) {
      return callback(new Error('请输入正确格式'))
    } else {
      callback()//必须有此项回调，否则验证会一直不通过
    }
  } else {
    callback()//必须有此项回调，否则验证会一直不通过
  }

}

//数字
const validateNumber = (rule, value, callback) => {
  let patter = /[0-9]*/
  if (!patter.test(value)) {
    return callback(new Error('请输入正确格式'))
  } else {
    callback()//必须有此项回调，否则验证会一直不通过
  }
}
/* userRules */
let user_passWorld = ''
const validatePass = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    let reg = /^[a-zA-Z0-9]{4,8}$/
    if (!reg.test(value)) {
      callback(new Error('密码为数字字母组合的4~8位'))
    } else {
      user_passWorld = value
      callback()
    }
  }
}
const validatePass2 = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== user_passWorld) {
    callback(new Error('两次输入密码不一致!'))
  } else {
    callback()
  }
}

export function checkMax20000(rule, value, callback) {
  if (value == '' || value == undefined || value == null) {
    callback()
  } else if (!Number(value)) {
    callback(new Error('请输入[1,20000]之间的数字'))
  } else if (value < 1 || value > 20000) {
    callback(new Error('请输入[1,20000]之间的数字'))
  } else {
    callback()
  }
}

export function isNumber(rule, value, callback) {
  if (value == '' || value == undefined || value == null) {
    callback(new Error('不能为空'))
  } else if (!Number(value)) {
    callback(new Error('请输入数字'))
  } else {
    callback()
  }
}

/* userRules end */
export default {
  common: { //公共验证方法
    mobilePhone: [{
      required: true,
      message: '手机号不能为空'
    }, {
      validator: validatePhone,
      trigger: 'blur'
    }],
    name: [{
      required: true,
      message: '姓名不能为空'
    }]
  },
  updatePwdRules: {
    oldPwd: [{
      required: true,
      message: '不能为空'
    }], newPwd1: [{
      required: true,
      message: '不能为空'
    }], newPwd2: [{
      required: true,
      message: '不能为空'
    }]
  },
  // 角色管理
  roleRules: {
    roleName: [{
      required: true,
      message: '请输入名称'
    }, {
      validator: validateRoleNameExist,
      trigger: 'blur'
    }], roleName2: [{
      required: true,
      message: '请输入名称',
      trigger: 'blur'
    }], moduleInfoList: [{
      type: 'array',
      required: true,
      message: '请选择权限',
      trigger: 'change'
    }]
  },
  // 用户管理
  userRules: {
    name: [{
      required: true,
      message: '请输入名称',
      trigger: 'blur'
    }],
    userName: [{
      required: true,
      message: '请输入登录账号'
    }, {
      validator: validateUserNameExist,
      trigger: 'blur'
    }], userName2: [{
      required: true,
      message: '请输入登录账号',
      trigger: 'blur'
    }],
    phone: [{
      required: true,
      message: '请输入电话',
      trigger: 'blur'
    }, {
      validator: validatePhone,
      trigger: 'blur'
    }],
    jobTitle: [{
      required: true,
      message: '请输入职位',
      trigger: 'blur'
    }],
    mail: [{
      required: true,
      message: '请输入邮箱',
      trigger: 'blur'
    }, {
      validator: validateEMail,
      trigger: 'blur'
    }],
    roleId: [{
      required: true,
      message: '请选择角色',
      trigger: 'change'
    }],
    organizationId: [{
      required: true,
      message: '请选择部门',
      trigger: 'change'
    }],
    warehouseList: [{
      type: 'array',
      required: true,
      message: '请选择所管仓库',
      trigger: 'blur'
    }]
  },
  // 器材添加管理
  equipmentRules: {
    name: [{
      required: true,
      message: '请输入名称',
      trigger: 'blur'
    }],
    model: [{
      required: true,
      message: '请选择型号',
      trigger: 'change'
    }],
    category: [{
      required: true,
      message: '请选择类别',
      trigger: 'change'
    }], equipmentTypeId: [{
      required: true,
      message: '请选择设备类别',
      trigger: 'change'
    }],
    labelType: [{
      required: true,
      message: '请选择标签类型',
      trigger: 'change'
    }],
    installMethod: [{
      required: true,
      message: '请选择安装方式',
      trigger: 'change'
    }],
    labelSize: [{
      required: true,
      message: '请选择标签规格',
      trigger: 'change'
    }], labelWay: [{
      required: true,
      message: '请选择贴标方式',
      trigger: 'change'
    }], equipmentImg: [{
      required: true,
      message: '请上传器材图片',
      trigger: 'blur'
    }], labelInstallImg: [{
      required: true,
      message: '请上传标签安装图片',
      trigger: 'blur'
    }]
  },
  // 器材预警设置
  warningRules: {
    safeStock: [{
      required: true,
      message: '不能为空'
    }, {
      validator: isNumber,
      trigger: 'blur'
    }],
    overdueDay: [{
      required: true,
      message: '不能为空'
    }, {
      validator: isNumber,
      trigger: 'blur'
    }]
  },
  // 器材预警设置
  purchaseEquipmentRules: {
    equipmentTypeId: [{
      type: 'array',
      required: true,
      message: '不能为空',
      trigger: 'change'
    }],
    productionDate: [{
      required: true,
      message: '不能为空',
      trigger: 'blur'
    }],
    expirationDate: [{
      required: true,
      message: '不能为空',
      trigger: 'blur'
    }],
    num: [{
      required: true,
      message: '不能为空'
    }, {
      validator: isNumber,
      trigger: 'blur'
    }],
    outStockNum: [{
      required: true,
      message: '不能为空'
    }, {
      validator: isNumber,
      trigger: 'blur'
    }],
    outNum: [{
      required: true,
      message: '不能为空'
    }, {
      validator: isNumber,
      trigger: 'blur'
    }]
  },
  // 器材分类
  equipmentTypeRules: {
    name: [{
      required: true,
      message: '请输入名称',
      trigger: 'blur'
    }]
  },
  // 采购单添加
  purchaseOrderRules: {
    batchNo: [{
      required: true,
      message: '请输入批次号',
      trigger: 'blur'
    }],
    operName: [{
      required: true,
      message: '请输入执行人',
      trigger: 'blur'
    }],
    purchaseNo: [{
      required: true,
      message: '请输入采购单号',
      trigger: 'blur'
    }],
    purchaseId: [{
      required: true,
      message: '请选择采购人',
      trigger: 'change'
    }]
  },
  warehouseInRules: {
    inNo: [{
      required: true,
      message: '入库单号不能为空',
      trigger: 'blur'
    }],
    warehouseId: [{
      required: true,
      message: '请选择存储仓库',
      trigger: 'change'
    }]
  },
  warehouseExRules: {
    outNo: [{
      required: true,
      message: '出库单号不能为空',
      trigger: 'blur'
    }],
    warehouseId: [{
      required: true,
      message: '请选择出库仓库',
      trigger: 'change'
    }],
    targetLocation: [{
      required: true,
      message: '请选择目标位置',
      trigger: 'change'
    }]
  },
  inventoryRules: {
    checkNo: [{
      required: true,
      message: '单号不能为空',
      trigger: 'blur'
    }],
    creator: [{
      required: true,
      message: '请选择发起人',
      trigger: 'blur'
    }],
    warehouseId: [{
      required: true,
      message: '请选择仓库',
      trigger: 'change'
    }],
    checkTime: [{
      required: true,
      message: '请选择盘库时间',
      trigger: 'change'
    }]
  }
}
